<?php


namespace app\api\controller;

use app\api\model\User as UserModel;
use app\api\model\Book as BookModel;
use app\api\model\Order as OrderModel;
use app\api\model\OrderItem as OrderItemModel;
/***
 * 书籍相关接口
 * Class Book
 * @package app\api\controller
 */
class Book extends Base
{
    /***
     * Desc :用户上传书籍
     * User : HAIYUN
     * Date : 2022-01-05 15:44
     */
    public function uploadBook()
    {
        //验证参数
        $rules = [
            ['book_name','require|unique:book','10001|20005'],
            ['price','require|number|>:0.00','10001|10002'],
            ['label','require|number','10001|10002'],
            ['number','require|number|>:0.00','10001|10002'],
            ['img','require','10001'],
        ];
        $msg   = $this->validate($this->params, $rules);
        if ($msg !== true) $this->return_msg($msg, '参数错误');
        //获取用户
        //根据token获取uid
        $uid = $this->getCurrentTokenVar("uid");
        //根据uid查找用户数据，判断是否存在
        $userModel = new UserModel();
        $user = $userModel->where("id",$uid)->find();
        if(!$user){
            $this->return_msg("20004","用户不存在",[]);
        }
        //新增数据
        $bookModel = new BookModel();
        $this->params['user_id'] = $uid;
        $book_info = $bookModel::create($this->params);
        if(!$book_info){
            $this->return_msg("20001","添加书籍失败",[]);
        }else{
            unset($book_info['user_id']);
            $this->return_msg("00000","添加书籍成功",$book_info);
        }
    }

    /***
     * Desc :用户上传书籍图片
     * User : HAIYUN
     * Date : 2022-01-05 17:30
     */
    public function uploadBookImg()
    {
        $url = config('setting.book_img_url');
        $type = 'book';
        $this->img_upload($url,$type);
    }

    /***
     * Desc :修改书籍
     * User : HAIYUN
     * Date : 2022-01-05 20:11
     */
    public function editBook()
    {
        //获取用户
        //根据token获取uid
        $uid = $this->getCurrentTokenVar("uid");
        //根据uid查找用户数据，判断是否存在
        $userModel = new UserModel();
        $user = $userModel->where("id",$uid)->find();
        if(!$user){
            $this->return_msg("20004","用户不存在",[]);
        }
        //验证参数
        $rules = [
            ['book_id','require','10001'],
            ['book_name','require|unique:book','10001|20005'],
            ['price','require|number|>:0.00','10001|10002'],
            ['number','require|number|>:0.00','10001|10002'],
            ['img','require','10001'],
        ];
        $msg   = $this->validate($this->params, $rules);
        if ($msg !== true) $this->return_msg($msg, '参数错误');
        //查询图书是否存在
        $where = [
            'user_id' => $uid,
            'id'=>$this->params['book_id'],
        ];
        $bookModel = new BookModel();
        $book = $bookModel->where($where)->find();
        if(!$book){
            $this->return_msg("20004","未查询到图书信息",[]);
        }
        $res = $bookModel->edit($this->params,['id'=>$this->params['book_id']]);
        if(!$res){
            $this->return_msg("20003","修改书籍失败",[]);
        }else{
            $this->return_msg("00000","修改书籍成功",$this->params);
        }
    }

    /***
     * Desc :查看书架
     * User : HAIYUN
     * Date : 2022-01-06 17:48
     */
    public function viewBookShelf()
    {
        //获取用户
        //根据token获取uid
        $uid = $this->getCurrentTokenVar("uid");
        //根据uid查找用户数据，判断是否存在
        $userModel = new UserModel();
        $user = $userModel->where("id",$uid)->find();
        if(!$user){
            $this->return_msg("20004","用户不存在",[]);
        }
        //查询用户上传的书籍
        $bookModel = new BookModel();
        $books = $bookModel->where('user_id',$uid)->field('id,book_name,author,price,discount,img')->select();
        $books_info = [
            'books' => $books,
            'total' => count($books),
        ];
        $this->return_msg("00000","查看书架成功",$books_info);
    }

    /***
     * Desc :用户下架书籍
     * User : HAIYUN
     * Date : 2022-01-06 17:57
     */
    public function undercarriage()
    {
        //获取用户
        //根据token获取uid
        $uid = $this->getCurrentTokenVar("uid");
        //根据uid查找用户数据，判断是否存在
        $userModel = new UserModel();
        $user = $userModel->where("id",$uid)->find();
        if(!$user){
            $this->return_msg("20004","用户不存在",[]);
        }
        //校验参数
        $rules = [
            ['book_id','require','10001']
        ];
        $msg   = $this->validate($this->params, $rules);
        if ($msg !== true) $this->return_msg($msg, '参数错误');
        $bookModel = new BookModel();
        $where = [
            'id' => $this->params['book_id'],
            'user_id' => $uid,
        ];
        $res = $bookModel->del($where);
        if(!$res){
            $this->return_msg("20003","下架书籍失败",[]);
        }else{
            $this->return_msg("00000","下架书籍成功",[]);
        }
    }

    /***
     * Desc :获取书籍信息
     * User : HAIYUN
     * Date : 2022-01-10 16:49
     */
    public function getBookInfo()
    {
        //验证参数
        $rules = [
            ['book_id','require','10001'],
        ];
        $msg   = $this->validate($this->params, $rules);
        if ($msg !== true) $this->return_msg($msg, '参数错误');
        $bookModel = new BookModel();
        $book_info = $bookModel->where('id',$this->params['book_id'])->find();
        if(!$book_info){
            $this->return_msg("20004","未查询到该书籍信息",[]);
        }else{
            //包装返回信息
            $return_data['book_info'] = $book_info;
            $return_data['user_info'] = [
                "user_id" => $book_info->user->id,
                "nick_name" => $book_info->user->nick_name,
                "avater" => $book_info->user->avater,
            ];
            $this->return_msg("00000","查询书籍信息成功",$return_data);
        }
    }

    /***
     * Desc:根据标签ID获取相关书籍
     * User: HAIYUN
     * Date: 2022/4/12 14:58
     */
    public function getBookByLabel() {
        //验证参数
        $rules = [
            ['label_id','require','10001'],
        ];
        $msg   = $this->validate($this->params, $rules);
        if ($msg !== true) $this->return_msg($msg, '参数错误');
        $bookModel = new BookModel();
        $book_info = $bookModel->where('label',$this->params['label_id'])->select();
        $this->return_msg("00000","根据标签查询相关书籍成功",$book_info);
    }

    /***
     * Desc:获取推荐书籍
     * User: HAIYUN
     * Date: 2022/4/12 15:07
     */
    public function getHotBook(){
        $show_num  = 2;
        $user_model = new UserModel();
        $user_list = $user_model->limit($show_num)->select();
        if(empty($user_model))
            $this->return_msg('00000','暂无推荐用户信息',[]);
        $return_data = [];
        foreach ($user_list as $k => $v){
            $return_data[$k]['user_id'] = $user_list[$k]['id'];
            $return_data[$k]['nick_name'] = $user_list[$k]['nick_name'];
            $return_data[$k]['avater'] = $user_list[$k]['avater'];
            $return_data[$k]['describe'] = $user_list[$k]['describe'];
            //查询该用户相关书架
            $bookModel = new BookModel();
            $books = $bookModel->where('user_id',$user_list[$k]['id'])->field('id,img')->select();
            $return_data[$k]['books'] = $books;
            $return_data[$k]['total'] = count($books);
        }
        $this->return_msg("00000","推荐书籍获取成功",$return_data);
    }

    /***
     * Desc:书籍搜索
     * User: HAIYUN
     * Date: 2022/4/13 15:16
     */
    public function searchBook() {
        //验证参数
        $rules = [
            ['name','require','10001'],
        ];
        $msg   = $this->validate($this->params, $rules);
        if ($msg !== true) $this->return_msg($msg, '参数错误');
        $bookModel = new BookModel();
        $book_info = $bookModel->where('book_name','like','%' . $this->params['name'].'%')->select();
        $this->return_msg("00000","搜索书籍成功",$book_info);
    }


    /***
     * Desc:书籍搜索(关键字,标签)
     * User: HAIYUN
     * Date: 2022/4/18 17:52
     */
    public function searchBookWithLabel() {
        //验证参数
        $rules = [
            ['name','require','10001'],
            ['label_id|number','require','10001|10002'],
        ];
        $msg   = $this->validate($this->params, $rules);
        if ($msg !== true) $this->return_msg($msg, '参数错误');
        $bookModel = new BookModel();
        $where = [
           'book_name' => ['like','%'. $this->params['name'].'%'],
           'label' => $this->params['label_id'],
        ];
        $book_list = $bookModel->where($where)->select();
        $return_data = [];
        foreach ($book_list as $k => $v){
            $return_data[$k]['id'] = $book_list[$k]['id'];
            $return_data[$k]['book_name'] = $book_list[$k]['book_name'];
            $return_data[$k]['author'] = $book_list[$k]['author'];
            $return_data[$k]['price'] = $book_list[$k]['price'];
            $return_data[$k]['img'] = $book_list[$k]['img'];
            $return_data[$k]['descibe'] = $book_list[$k]['descibe'];
        }
        $this->return_msg("00000","搜索书籍成功",$return_data);
    }

    /***
     * Desc:获取用户已售书籍
     * User: HAIYUN
     * Date: 2022/4/18 18:40
     */
    public function getSoldBook() {
        //验证参数
        $rules = [
            ['user_id','require','10001'],
        ];
        $msg   = $this->validate($this->params, $rules);
        if ($msg !== true) $this->return_msg($msg, '参数错误');
        //先查询该用户订单
        $orderModel = new OrderModel();
        $where = [
            'user_id' => $this->params['user_id'],
            'is_buyer' => 0,
        ];
        $orders = $orderModel->where($where)->select();
        //将uuid组合成一个数组
        $uuids = [];
        foreach ($orders as $k => $v){
            $uuids[$k] = $orders[$k]['uuid'];
        }
        //查询订单详情
        $orderItemModel = new OrderItemModel();
        $items = $orderItemModel->where('order_uuid','in',$uuids)->group('item_id')->select();
        $return_data = [];
        foreach ($items as $k => $v){
            $return_data[$k]['book_id'] = $items[$k]['item_id'];
            $return_data[$k]['book_name'] = $items[$k]['title'];
            $return_data[$k]['img'] = $items[$k]['pic_path'];
            $return_data[$k]['price'] = $items[$k]['price'];
        }

//        $return_data = [];
//        $orderItemModel = new OrderItemModel();
//        foreach ($orders as $k => $v){
//            //根据uuid查询订单详情
//            $items = $orderItemModel->where(['order_uuid'=>$orders[$k]['uuid']])->select();
//            if(!is_null($items)){
//                foreach ($items as $item_k => $item_v){
//                    $return_data[$k]['book_id'] = $items[$item_k]['item_id'];
//                    $return_data[$k]['book_name'] = $items[$item_k]['title'];
//                    $return_data[$k]['image'] = $items[$item_k]['pic_path'];
//                }
//            }
//        }
        $this->return_msg('00000','查询已售书籍成功',$return_data);
    }

    /***
     * Desc:根据用户id获取书架
     * User: HAIYUN
     * Date: 2022/4/19 11:07
     */
    public function getBookByUserId() {
        //验证参数
        $rules = [
            ['user_id','require','10001'],
        ];
        $msg   = $this->validate($this->params, $rules);
        if ($msg !== true) $this->return_msg($msg, '参数错误');
        //查询用户上传的书籍
        $bookModel = new BookModel();
        $books = $bookModel->where('user_id',$this->params['user_id'])->field('id,book_name,author,price,discount,img')->select();
        $books_info = [
            'books' => $books,
            'total' => count($books),
        ];
        $this->return_msg("00000","查看书架成功",$books_info);
    }
}